﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ListFramework.Models;

namespace ListFramework.ViewModels
{
    public class MainViewModel
    {
        private ObservableCollection<Item> _items;

        public ObservableCollection<Item> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        private ObservableCollection<FilterCriteria> _filterCriterias;

        public ObservableCollection<FilterCriteria> FilterCriterias
        {
            get { return _filterCriterias; }
            set { _filterCriterias = value; }
        }

        public MainViewModel()
        {
            populateFilterBehaviours();
            populateItems();
        }

        public FilterCriteria SelectedFilterCriteria { get; set; }

        private void populateFilterBehaviours()
        {
            //Setzen der Filteroptionen (Verhalten entweder Beides, Wert1 oder Wert2)
            _filterCriterias = new ObservableCollection<FilterCriteria>();
            _filterCriterias.Add(new FilterCriteria("Beides", FilterBehaviour.Both));
            _filterCriterias.Add(new FilterCriteria("Kennzeichen",FilterBehaviour.Value1));
            _filterCriterias.Add(new FilterCriteria("Stadt", FilterBehaviour.Value2));
            //Initial ausgewählte Filterart
            SelectedFilterCriteria = _filterCriterias[0];
        }

        private void populateItems()
        {
            //Einlesen der CSV Datei
            _items = new ObservableCollection<Item>();
            var streamInfo = Application.GetResourceStream(new Uri("Items/items.csv", UriKind.Relative));
            StreamReader reader = new StreamReader(streamInfo.Stream);
            while(!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                char[] sep = {';'};
                var words = line.Split(sep);
                if(words.Length == 2)
                {
                    _items.Add(new Item(words[0],words[1]));
                }
            }
        }
    }
}
